﻿using System;
using System.Data;


namespace CarBlackBox
{
    [Serializable]
    public class CarTypeData
    {

        public const string ID_Field = @"ID";

        public const string NAME_Field = @"Name";

        private Byte _ID;

        private String _Name;

        public Byte ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        public String Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        public CarTypeData()
        {
            _ID = 0;
            _Name = "";
        }
    }

    public class CarTypeDataFactory
    {
        public static CarTypeData Construct(IDataReader reader)
        {
            CarTypeData data = new CarTypeData();

            data.ID = Convert.ToByte(reader["ID"]);

            data.Name = Convert.ToString(reader["Name"]);

            return data;
        }
        public static CarTypeData Construct(DataTable dt)
        {
            CarTypeData data = new CarTypeData();

            if (dt.Rows.Count == 0)

                return null;

            DataRow dr = dt.Rows[0];

            if (dr == null)

                return null;

            try { data.ID = Convert.ToByte(dr["ID"]); }
            catch { }

            try { data.Name = Convert.ToString(dr["Name"]); }
            catch { }

            return data;
        }
        public static CarTypeData Construct(DataTable dt, int rowIndex)
        {
            CarTypeData data = new CarTypeData();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.ID = Convert.ToByte(dr["ID"]); }
            catch { }

            try { data.Name = Convert.ToString(dr["Name"]); }
            catch { }

            return data;
        }
    }
}